﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using xTeam.HengLe.Entity;
using System.Web.Script.Serialization;
using Valor.Helper;
using xTeam.HengLe.Enums;

namespace xTeam.HengLe.Helper
{
    public static class HtmlExtned
    {
        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="classifyList"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static string DropDownlist(string name, List<EClassify> classifyList, int selectedValue)
        {
            return DropDownlist(name, classifyList, selectedValue, false);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="classifyList"></param>
        /// <param name="selectedValue"></param>
        /// <param name="please"></param>
        /// <returns></returns>
        public static string DropDownlist(string name, List<EClassify> classifyList, int selectedValue, bool please)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<label>");
                        sb.AppendFormat("<input type=\"radio\" title=\"{0}\"", i.Title);
                        if (selectedValue == i.ClassifyId)
                            sb.Append(" checked=\"checked\"");
                        sb.Append(" name=\"" + name + "\"");
                        sb.Append(" value=\"" + i.ClassifyId + "\" />");

                        sb.Append(i.Title);
                        sb.Append("</label>");
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            if (please)
            {
                sb.Append("<li>");
                sb.Append("<div class=\"overFlowHidden\">");
                sb.Append("<label>");
                sb.AppendFormat("<input type=\"radio\" title=\"顶级分类\"");
                sb.Append(" checked=\"checked\"");
                sb.Append(" name=\"" + name + "\"");
                sb.Append(" value=\"0\" />");
                sb.Append("顶级分类");
                sb.Append("</label>");
                sb.Append("</div>");
            }
            if (classifyList.Count > 0) {
                tree(classifyList, classifyList.Min(i => i.ParentId));
            }
            //sb.Append("</ul>");
            return sb.ToString();

        }
        /// <summary>
        /// 类别选择器，只有最有一级类别可选
        /// </summary>
        /// <param name="name"></param>
        /// <param name="selectedValue"></param>
        /// <param name="classifyList"></param>
        /// <returns></returns>
        public static string ParentClassify(string name, int selectedValue, List<EClassify> classifyList)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<label>");
                        sb.AppendFormat("<input type=\"radio\" title=\"{0}\"", i.Title);
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                            sb.Append(" disabled=\"disabled\"");
                        if (selectedValue == i.ClassifyId)
                            sb.Append(" checked=\"checked\"");
                        sb.Append(" name=\"" + name + "\"");
                        sb.Append(" value=\"" + i.ClassifyId + "\" />");

                        sb.Append(i.Title);
                        sb.Append("</label>");
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            sb.Append("<li>");
            sb.Append("<div class=\"overFlowHidden\">");
            sb.Append("<label>");
            sb.AppendFormat("<input type=\"radio\" title=\"请选择分类\"");
            sb.Append(" checked=\"checked\" disabled=\"disabled\"");
            sb.Append(" name=\"" + name + "\"");
            sb.Append(" value=\"0\" />");
            sb.Append("请选择分类");
            sb.Append("</label>");
            sb.Append("</div>");
            // 最底层
            tree(classifyList,classifyList.Min(i=>i.ParentId));
            //sb.Append("</ul>");
            return sb.ToString();

        }
        /// <summary>
        /// 生成树
        /// </summary>
        /// <param name="classifyList"></param>
        /// <returns></returns>
        public static string ClassTree(List<EClassify> classifyList)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<div class=\"floatRight\">");
                        sb.Append("<input type=\"text\" disabled=\"disabled\" value=\"" + i.Sequence + "\" class=\"width50 textCenter\" />");
                        sb.Append("<input type=\"button\" value=\"编辑\" name=\"btnEdit\" onclick=\"Edit(" + i.ClassifyId + ")\" />");
                        sb.Append("<input type=\"button\" value=\"删除\" name=\"btnDelete\" onclick=\"Delete(" + i.ClassifyId + ")\" />");
                        sb.Append("</div>");
                        sb.Append(i.Title);
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            if (classifyList.Count > 0) {
                tree(classifyList, classifyList.Min(i => i.ParentId));
            }
            //sb.Append("</ul>");
            return sb.ToString();
        }
        /// <summary>
        /// 专用菜单
        /// </summary>
        /// <param name="html"></param>
        /// <param name="classifyList"></param>
        /// <returns></returns>
        public static string HengLeMenu(this HtmlHelper html, List<EClassify> classifyList)
        {
            StringBuilder sb = new StringBuilder();
            Action<int> tree = null;
            tree = (int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        bool isChild = classifyList.Count(n => n.ParentId == i.ClassifyId) > 0;
                        sb.AppendFormat("<li{0}>", isChild ? " class=\"parent\"" : string.Empty);
                        sb.Append("<div class=\"title parent\">");
                        sb.Append(string.IsNullOrEmpty(i.Redirect)
                            ? (object)html.ActionLink(i.Title, "Index", i.ModuleKey, new { classid = i.ClassifyId }, null)
                            : string.Format("<a href=\"{1}\">{0}</a>", i.Title, i.Redirect));
                        sb.Append("</div>");

                        if (isChild)
                        {
                            sb.Append("<div class=\"childBox\">");
                            sb.Append("<ul>");
                            tree(i.ClassifyId);
                            sb.Append("</ul>");
                            sb.Append("</div>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            if (classifyList.Count > 0) {
                tree(classifyList.Min(i => i.ParentId));
            }
            //sb.Append("</ul>");
            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AdType(int value)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            List<SelectListItem> adTypeList = json.Deserialize<List<SelectListItem>>(json.Serialize(EnumHelper.GetEnumsToList(typeof(AdType))));
            return adTypeList.SingleOrDefault(i => Convert.ToInt32(i.Value) == value).Text;
        }
    }
}
